package com.kevinwu.zookeepertask.taskone.client;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.retry.ExponentialBackoffRetry;

import java.util.List;
import java.util.concurrent.CountDownLatch;

public class Test {

    private  static CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) throws Exception {

        RetryPolicy exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3);
        // 使用fluent编程风格
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString("192.168.33.19:2181")
                .sessionTimeoutMs(50000)
                .connectionTimeoutMs(30000)
                .retryPolicy(exponentialBackoffRetry)
                .namespace("app")  // 独立的命名空间 /base
                .build();
        client.start();
        List<String> forPath = client.getChildren().forPath("/servers");
        System.out.println("节点下所有子节点："+forPath);
        watchChild(client,"/servers");
        countDownLatch.await();

    }

    protected static void watchChild(CuratorFramework client,String path) throws Exception {
        //创建PathChildrenCache
        //参数：true代表缓存数据到本地
        PathChildrenCache pathChildrenCache = new PathChildrenCache(client,path,true);
        //BUILD_INITIAL_CACHE 代表使用同步的方式进行缓存初始化。
        pathChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
        pathChildrenCache.getListenable().addListener((cf, event) -> {
            PathChildrenCacheEvent.Type eventType = event.getType();
            switch (eventType) {
                case CONNECTION_RECONNECTED:
                    pathChildrenCache.rebuild();
                    System.out.println("****************************:"+new String(event.getData().getData()));
                    break;
                case CONNECTION_SUSPENDED:
                    break;
                case CONNECTION_LOST:
                    System.out.println("Connection lost");
                    System.out.println("****************************:"+new String(event.getData().getData()));
                    break;
                case CHILD_ADDED:
                    System.out.println("Child added");
                    System.out.println("****************************:"+new String(event.getData().getData()));
                    String strs[] = new String(event.getData().getData()).split(":");
                    System.out.println("IP地址为："+strs[0]+"端口为："+strs[1]);
                    break;
                case CHILD_UPDATED:
                    System.out.println("Child updated");
                    System.out.println("****************************:"+new String(event.getData().getData()));
                    break;
                case CHILD_REMOVED:
                    System.out.println("Child removed");
                    System.out.println("****************************:"+new String(event.getData().getData()));
                    String strs1[] = new String(event.getData().getData()).split(":");
                    System.out.println("IP地址为："+strs1[0]+"端口为："+strs1[1]);
                    break;
                default:
            }
        });
    }
}
